package com.chezai.client.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.chezai.client.bean.ClassRecord;

import com.chezai.client.bean.RandomPhoto;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "RANDOM_PHOTO".
*/
public class RandomPhotoDao extends AbstractDao<RandomPhoto, Long> {

    public static final String TABLENAME = "RANDOM_PHOTO";

    /**
     * Properties of entity RandomPhoto.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property _ID = new Property(0, Long.class, "_ID", true, "_id");
        public final static Property PhotoPath = new Property(1, String.class, "photoPath", false, "PHOTO_PATH");
        public final static Property ClassRecordId = new Property(2, long.class, "classRecordId", false, "CLASS_RECORD_ID");
        public final static Property _0305 = new Property(3, String.class, "_0305", false, "_0305");
        public final static Property UploadStatus = new Property(4, int.class, "uploadStatus", false, "UPLOAD_STATUS");
        public final static Property TryCount = new Property(5, int.class, "tryCount", false, "TRY_COUNT");
        public final static Property TakePhotoTimeStamp = new Property(6, Long.class, "takePhotoTimeStamp", false, "TAKE_PHOTO_TIME_STAMP");
        public final static Property IsEndSupplementPhoto = new Property(7, String.class, "isEndSupplementPhoto", false, "IS_END_SUPPLEMENT_PHOTO");
    }

    private DaoSession daoSession;


    public RandomPhotoDao(DaoConfig config) {
        super(config);
    }
    
    public RandomPhotoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"RANDOM_PHOTO\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: _ID
                "\"PHOTO_PATH\" TEXT," + // 1: photoPath
                "\"CLASS_RECORD_ID\" INTEGER NOT NULL ," + // 2: classRecordId
                "\"_0305\" TEXT," + // 3: _0305
                "\"UPLOAD_STATUS\" INTEGER NOT NULL ," + // 4: uploadStatus
                "\"TRY_COUNT\" INTEGER NOT NULL ," + // 5: tryCount
                "\"TAKE_PHOTO_TIME_STAMP\" INTEGER," + // 6: takePhotoTimeStamp
                "\"IS_END_SUPPLEMENT_PHOTO\" TEXT);"); // 7: isEndSupplementPhoto
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RANDOM_PHOTO\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, RandomPhoto entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        String photoPath = entity.getPhotoPath();
        if (photoPath != null) {
            stmt.bindString(2, photoPath);
        }
        stmt.bindLong(3, entity.getClassRecordId());
 
        String _0305 = entity.get_0305();
        if (_0305 != null) {
            stmt.bindString(4, _0305);
        }
        stmt.bindLong(5, entity.getUploadStatus());
        stmt.bindLong(6, entity.getTryCount());
 
        Long takePhotoTimeStamp = entity.getTakePhotoTimeStamp();
        if (takePhotoTimeStamp != null) {
            stmt.bindLong(7, takePhotoTimeStamp);
        }
 
        String isEndSupplementPhoto = entity.getIsEndSupplementPhoto();
        if (isEndSupplementPhoto != null) {
            stmt.bindString(8, isEndSupplementPhoto);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, RandomPhoto entity) {
        stmt.clearBindings();
 
        Long _ID = entity.get_ID();
        if (_ID != null) {
            stmt.bindLong(1, _ID);
        }
 
        String photoPath = entity.getPhotoPath();
        if (photoPath != null) {
            stmt.bindString(2, photoPath);
        }
        stmt.bindLong(3, entity.getClassRecordId());
 
        String _0305 = entity.get_0305();
        if (_0305 != null) {
            stmt.bindString(4, _0305);
        }
        stmt.bindLong(5, entity.getUploadStatus());
        stmt.bindLong(6, entity.getTryCount());
 
        Long takePhotoTimeStamp = entity.getTakePhotoTimeStamp();
        if (takePhotoTimeStamp != null) {
            stmt.bindLong(7, takePhotoTimeStamp);
        }
 
        String isEndSupplementPhoto = entity.getIsEndSupplementPhoto();
        if (isEndSupplementPhoto != null) {
            stmt.bindString(8, isEndSupplementPhoto);
        }
    }

    @Override
    protected final void attachEntity(RandomPhoto entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public RandomPhoto readEntity(Cursor cursor, int offset) {
        RandomPhoto entity = new RandomPhoto( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // _ID
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // photoPath
            cursor.getLong(offset + 2), // classRecordId
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // _0305
            cursor.getInt(offset + 4), // uploadStatus
            cursor.getInt(offset + 5), // tryCount
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // takePhotoTimeStamp
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7) // isEndSupplementPhoto
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, RandomPhoto entity, int offset) {
        entity.set_ID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setPhotoPath(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setClassRecordId(cursor.getLong(offset + 2));
        entity.set_0305(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setUploadStatus(cursor.getInt(offset + 4));
        entity.setTryCount(cursor.getInt(offset + 5));
        entity.setTakePhotoTimeStamp(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setIsEndSupplementPhoto(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(RandomPhoto entity, long rowId) {
        entity.set_ID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(RandomPhoto entity) {
        if(entity != null) {
            return entity.get_ID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(RandomPhoto entity) {
        return entity.get_ID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getClassRecordDao().getAllColumns());
            builder.append(" FROM RANDOM_PHOTO T");
            builder.append(" LEFT JOIN CLASS_RECORD T0 ON T.\"CLASS_RECORD_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected RandomPhoto loadCurrentDeep(Cursor cursor, boolean lock) {
        RandomPhoto entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ClassRecord classRecord = loadCurrentOther(daoSession.getClassRecordDao(), cursor, offset);
         if(classRecord != null) {
            entity.setClassRecord(classRecord);
        }

        return entity;    
    }

    public RandomPhoto loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<RandomPhoto> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<RandomPhoto> list = new ArrayList<RandomPhoto>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<RandomPhoto> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<RandomPhoto> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
